package com.yls.except.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

/**
 * 用户dao
 *
 * @author xs
 * @version 1.0
 * @time 2016-1-25 下午5:49:31
 * @Description: {}
 */
@Repository(value = "userDao")
public interface UserDao {

    /**
     * 根据openid和绑定类型查询三方绑定信息
     *
     * @param map openid和绑定类型
     * @return 三方绑定信息
     */
    @Select("select uid,binding_num,binding_type,nick_name,sex,binding_date,head,isdisabled,relieve_date from yls_three_binding where binding_num=#{binding_num} and binding_type=#{binding_type}")
    Map<String, Object> selectThreeInfo(Map<String, Object> map);

    /**
     * 新增用户信息
     *
     * @param userinfo 用户信息
     * @return 新增状态（大于0成功，否则失败）
     */
    @Insert("insert into yls_user " +
            "(nickname,phone,userpwd,type,username,headimg,sex,birthday,age,registertime,registertip,isdisabled) " +
            "values " +
            "(#{nickname},#{phone},#{userpwd},#{type},#{username},#{headimg},#{sex},#{birthday},#{age},#{registertime},#{registertip},#{isdisabled})")
    @Options(useGeneratedKeys = true, keyProperty = "uid")
    int insertUser(Map<String, Object> userinfo);

    /**
     * 新增三方绑定信息
     *
     * @param threeBanding 三方绑定信息
     * @return 新增状态（大于0成功，否则失败）
     */
    @Insert("insert into yls_three_binding " +
            "(uid,binding_num,binding_type,nick_name,sex,binding_date,head,isdisabled,relieve_date) " +
            "values " +
            "(#{uid},#{binding_num},#{binding_type},#{nick_name},#{sex},#{binding_date},#{head},#{isdisabled},#{relieve_date}) ")
    int insertThreeBing(Map<String, Object> threeBanding);

    /**
     * 新增用户登录记录信息
     *
     * @param userloginlog 用户登录记录信息
     * @return 新增状态（大于0成功，否则失败）
     */
    @Insert("insert into yls_user_loginlog " +
            "(uid,currentlng,currentlat,loginlng,loginlat,lastloginlng,lastloginlat,logintime,loginip,exittime,exitip,lastexittime,lastexitip,logindate,logindateavg,firstlogintime,firstloginip,state,lastlogintime,lastloginip,count,isdisabled) " +
            "values " +
            "(#{uid},#{currentlng},#{currentlat},#{loginlng},#{loginlat},#{lastloginlng},#{lastloginlat},#{logintime},#{loginip},#{exittime},#{exitip},#{lastexittime},#{lastexitip},#{logindate},#{logindateavg},#{firstlogintime},#{firstloginip},#{state},#{lastlogintime},#{lastloginip},#{count},#{isdisabled})")
    int insertUserLogin(Map<String, String> userloginlog);

    /**
     * 根据用户id查询用户信息
     *
     * @param uid 用户id
     * @return 用户信息
     */
    @Select("select uid,nickname,phone,userpwd,type,username,headimg,sex,birthday,age,registertime,registertip,invite_url,isdisabled from yls_user where uid=#{uid} and isdisabled=1")
    Map<String, Object> selectUserInfoByUid(String uid);

    /**
     * 根据用户id查询用户登录信息
     *
     * @param uid 用户id
     * @return 用户登录信息
     */
    @Select("select " +
            "uid,currentlng,currentlat,loginlng,loginlat,lastloginlng,lastloginlat,logintime,loginip,exittime,exitip,lastexittime,lastexitip,logindate,logindateavg,firstlogintime,firstloginip,state,lastlogintime,lastloginip,count,isdisabled " +
            "from " +
            "yls_user_loginlog " +
            "where " +
            "uid=#{uid}")
    Map<String, Object> selectUserLoginLogByUid(String uid);

    /**
     * 修改用户登录记录信息
     * @param userloginlog 登录记录信息
     * @return 修改状态（大于0成功，否则失败）
     */
    @Update("update yls_user_loginlog set " +
            "currentlng=#{currentlng},currentlat=#{currentlat},loginlng=#{loginlng},loginlat=#{loginlat},lastloginlng=#{lastloginlng},lastloginlat=#{lastloginlat},logintime=#{logintime}," +
            "loginip=#{loginip},exittime=#{exittime},exitip=#{exitip},lastexittime=#{lastexittime},lastexitip=#{lastexitip},logindate=#{logindate},logindateavg=#{logindateavg}," +
            "firstlogintime=#{firstlogintime},firstloginip=#{firstloginip},state=#{state},lastlogintime=#{lastlogintime},lastloginip=#{lastloginip},count=#{count},isdisabled=#{isdisabled} " +
            "where uid=#{uid}")
    int updateUserLoginLogInfo(Map<String, Object> userloginlog);

    /**
     * 根据用户id查询用户帐户信息
     * @param uid 用户id
     * @return 用户帐户信息
     */
    @Select("select account_num,uid,sparemoney,paypwd,addtime,addip,isdisabled from yls_user_account where uid=#{uid}")
    Map<String,Object> selectUserAccountByUid(String uid);

    /**
     * 修改用户账户信息
     * @param account 用户帐户信息
     * @return 修改状态（大于0成功，否则失败）
     */
    @Update("update yls_user_account set " +
            "uid=#{uid},sparemoney=#{sparemoney},paypwd=#{paypwd},addtime=#{addtime},addip=#{addip},isdisabled=#{isdisabled} where account_num=#{account_num}")
    int updateUserAccount(Map<String, Object> account);

    /**
     * 修改用户个人资料
     * @param userinfo 用户个人资料
     * @return 修改状态（大于0成功，否则是吧）
     */
    @Update("update yls_user set " +
            "nickname=#{nickname},phone=#{phone},userpwd=#{userpwd},type=#{type},username=#{username},headimg=#{headimg},sex=#{sex},birthday=#{birthday},age=#{age},registertime=#{registertime},registertip=#{registertip},invite_url=#{invite_url},isdisabled=#{isdisabled} " +
            "where uid=#{uid}")
    int updateUserInfo(Map<String, Object> userinfo);

    /**
     * 新增推荐信息
     * @param invite 推荐信息
     * @return 新增状态（大于0成功，否则失败）
     */
    @Insert("insert into yls_invite " +
            "(invite_id,invite_uid,uid,addtime,addip,isdisabled) " +
            "values " +
            "(#{invite_id},#{invite_uid},#{uid},#{addtime},#{addip},#{isdisabled})")
    int insertInviteInfo(Map<String, Object> invite);

    /**
     * 根据用户id查询该用户推荐的用户列表信息
     * @param map 用户id
     * @return 用户推荐的用户列表信息
     */
    @Select("select invite_id,invite_uid,uid,addtime,addip,isdisabled from yls_invite where invite_uid=#{uid} and isdisabled=1 order by addtime desc limit #{start}, #{count}")
    List<Map<String,Object>> selectInviteInfo(Map<String, Object> map);

    /**
     * 根据用户用户id和获取用户id查询消费信息
     * @param inviteinfo 推荐信息
     * @return 消费信息
     */
    @Select("select ifnull(sum(i.indent_money), 0) indentmoney,ifnull(sum(i.percentage), 0) percentage, u.nickname  " +
            "from yls_invite_money_log i,yls_user u where i.indent_uid=#{uid} and i.uid=#{invite_uid} and u.uid=#{uid} and i.isdisabled=1")
    Map<String,Object> selectProxyMoneyByUid(Map<String, Object> inviteinfo);

    /**
     * 根据用户id查询推荐用户信息
     * @param uid 用户id
     * @return 推荐用户信息
     */
    @Select("select invite_id,invite_uid,uid,addtime,addip,isdisabled from yls_invite where uid=#{uid} and isdisabled=1")
    Map<String,Object> selectInviteInfoByUid(String uid);

    /**
     * 新增用户帐户信息
     * @param useraccout 用户帐户信息
     * @return 用户帐户信息
     */
    @Insert("insert into yls_user_account " +
            "(account_num,uid,sparemoney,paypwd,addtime,addip,isdisabled) " +
            "values " +
            "(#{account_num},#{uid},#{sparemoney},#{paypwd},#{addtime},#{addip},#{isdisabled})")
    int insertUserAccount(Map<String, Object> useraccout);

    /**
     * 根据用户i的查询用户认证信息
     * @param uid 用户id
     * @return 用户认证信息
     */
    @Select("select auth_id,uid,name,card,authority,expiredate,addtime,addip,status,updateaid,updateip,remark,isdisabled from yls_auth where uid=#{uid}")
    Map<String,Object> selectUserAuthInfoByUid(String uid);

    /**
     * 新增实名认证信息
     * @param auth 认证id
     * @return 新增状态（大于0成功，否则失败）
     */
    @Insert("insert into yls_auth " +
            "(uid,name,card,addtime,addip,isdisabled) " +
            "values(#{uid},#{name},#{card},#{addtime},#{addip},#{isdisabled})")
    int insertUserAuthInfo(Map<String, Object> auth);
}